package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.StatusCount;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    int insert(Orders order);


    /**
     * 根据订单号查询订单
     *
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);


    /**
     * 替换威胁你支付更新数据库状态问题
     *
     * @param orderStatus
     * @param orderPaidStatus
     * @param checkOutTime
     * @param orderNumber
     */
    @Update("update orders set status = #{orderStatus},pay_status=#{orderPaidStatus},checkout_time=#{checkOutTime}  where number=#{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, String orderNumber);

    /**
     * 分页查询
     *
     * @param order
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO order);


    /**
     * 根据id查询订单
     *
     * @param orderId
     * @return
     */
    @Select("select * from orders where  id =#{orderId}")
    Orders getById(Integer orderId);

    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @Select("select status,count(*) AS count from orders group by status")
    List<StatusCount> CountsByStatus();

    /**
     * 获得超时订单
     * @return
     */
    @Select("select * from orders where order_time <= #{orderTime} and status = #{status}")
    List<Orders> getByStatusAndOrderTimeLE(LocalDateTime orderTime, Integer status);

    /**
     * 根据状态获取
     * @param status
     * @return
     */
    @Select("select * from orders where status=#{status}")
    List<Orders> getByStatus(Integer status);

    /**
     * 订单额统计
     * @param map
     * @return
     */
    @Select("select sum(amount) from orders where order_time< #{endTime} and order_time > #{beginTime} and status = #{status};")
    Double sumByMap(Map map);


    /**
     * 订单数量统计
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
